<?php
/**
* @desc curl 方法的实现
* @version 1.0
*
*/
namespace Org\Net;
class Curl {
 
	/**
	* url地址
	*
	* @var unknown_type
	*/
	private $_url = '';
	private $_cookie = '';
	public $httpCode = '';
	public $curlNo = '';
	public $curlError = '';
 
	function __construct($url = '') {
		if (!function_exists('curl_init'))
		die('请开启Curl模块');
		$this->_url = $url;
	}
	static function instance()
	{
		static $obj;
		if(is_null($obj))
		{
			$obj = new self();
		}
		return $obj;
	
	}
	/**
	* 私有变量赋值
	*
	* @param unknown_type $name
	* @param unknown_type $value
	*/
	public function setUrl($value) {
		$this->_url = $value;
	}
 
	/**
	* 设置cookie的值
	* 形如：hello1=111;hello2=222;hello3=333;hello4=444
	* @param unknown_type $value
	*/
	public function setCookie($value) {
		$this->_cookie = $value;
	}
 
	/**
	* 获取私有变量的值
	*
	* @param unknown_type $name
	* @return unknown
	*/
	public function getUrl() {
		return $this->_url;
	}
 
	protected function getTransferType($type = 'json',$data=''){
		$arr = array(
				'json'=>array(
						'Content-Type: application/json',
						'Content-Length: ' . strlen($data)
					),
				'common'=>array(
						'Content-Type: application/x-www-form-urlencoded'
					),
		);
		return isset($arr[$type]) ? $arr[$type] :'';
	}
	/**
	* 将data post到指定的url
	* transferType  类型      json eg: {"lotteryId":1}|common  eg:name=aboc&num=bbb
	* @param $data array/str都可以
	* @return unknown
	*/
	public function post($data, $user_agent = false,$transferType = 'json') {
		if (is_array($data)) {
			$str = array();
			foreach ($data as $key => $value) {
				if (!is_array($value))
				$str[] = $key . '=' . urlencode($value);
			}
			$str = join('&', $str);
		}
		else {
			$str = $data;
		}
		$transfer = $this->getTransferType($transferType,$data);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->_url);
		curl_setopt($ch, CURLOPT_POST, count($data));
		curl_setopt($ch, CURLOPT_POSTFIELDS, $str);

		curl_setopt($ch, CURLOPT_HTTPHEADER, $transfer);
		if ($user_agent) {
			curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0");
		}
		if (!empty($this->_cookie))
			curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie);
		ob_start();
		if (curl_exec($ch)) {
			$result = ob_get_contents();
		} else {
			$result = false;
		}
		$this->httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
		if(curl_errno($ch)){
			$this->curlNo = curl_errno($ch);
			$this->curlError = $this->getCurlError($ch);
		}
		if($this->httpCode == '404'){
			$this->curlNo = '404';
			$this->curlError = $this->getCurlError();
		}
		ob_end_clean();
		curl_close($ch);
		return $result;
	}
 
	public function get($user_agent = false,$sendip='') {
		$ch = curl_init($this->_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
		//伪造客户端IP
		if($sendip!="")
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$sendip, 'CLIENT-IP:'.$sendip));
 
		if ($user_agent) {
			curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0");
		}
		if (!empty($this->_cookie))
			curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie);
		$result = curl_exec($ch);
		$this->httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
		if(curl_errno($ch)){
			$this->curlNo = curl_errno($ch);
			$this->curlError = $this->getCurlError($ch);
		}
		if($this->httpCode == '404'){
			$this->curlNo = '404';
			$this->curlError = $this->getCurlError();
		}
		curl_close($ch);
		return $result;
	}
 	
	function getCurlError($ch){
		$msgList = array(
				'3' => 'URL 格式错误',
				'6' => '底层服务地址迁移',
				'7' => '系统维护中，请稍后提交订单',
				'28' => '访问超时',
				'43' => '内部错误',
				'56' => '接收网络数据时失败',
				'404' => '服务不存在',
		);
		return isset($msgList[$this->curlNo]) ? $msgList[$this->curlNo] : curl_error($ch);
	}
	
	function __destruct() {
 
	}
}
?>